<?php

/* 问卷  */

class F_Usuwj extends A_Field {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        $this->name = fc_lang('问卷调查字段-优速软件定制'); // 字段名称
        $this->fieldtype = array(
            'TEXT' => ''
        ); // TRUE表全部可用字段类型,自定义格式为 array('可用字段类型名称' => '默认长度', ... )
        $this->defaulttype = 'TEXT'; // 当用户没有选择字段类型时的缺省值
    }

    /**
     * 字段相关属性参数
     *
     * @param	array	$value	值
     * @return  string
     */
    public function option($option) {

        $width = isset($option['width']) ? $option['width'] : '99%';

        $str = '
		<div class="form-group">
			<label class="col-md-2 control-label">'.fc_lang('宽度').'：</label>
			<div class="col-md-9">
				<label><input type="text" class="form-control" size="10" name="data[setting][option][width]" value="'.$width.'"></label>
				<span class="help-block">'.fc_lang('[整数]表示固定宽带；[整数%]表示百分比').'</span>
			</div>
		</div>';

        return $str.
            '<div class="form-group">
				<label class="col-md-2 control-label">'.fc_lang('友情提示').'：</label>
				<div class="col-md-9" style="color:blue"> <div class="form-control-static">'.fc_lang('此字段不能参与搜索条件筛选').'</div></div>
			</div>';
    }

    /**
     * 字段输出
     */
    public function output($value) {
        return dr_string2array($value);
    }

    /**
     * 字段入库值
     */
    public function insert_value($field) {

        $data = array();
        $value = $this->ci->post[$field['fieldname']];
        if ($value) {
            $i = 1;
            foreach ($value as $t) {
                $data[$i] = $t;
                $i++;
            }
        }


        $this->ci->data[$field['ismain']][$field['fieldname']] = dr_array2string($data);
    }

    /**
     * 字段表单输入
     *
     * @param	string	$cname	字段别名
     * @param	string	$name	字段名称
     * @param	array	$cfg	字段配置
     * @param	string	$value	值
     * @return  string
     */
    public function input($cname, $name, $cfg, $value = NULL, $id = 0) {
        // 字段显示名称
        $text = (isset($cfg['validate']['required']) && $cfg['validate']['required'] == 1 ? '<font color="red">*</font>' : '').''.$cname.'：';
        // 显示框宽度设置
        $width = isset($cfg['option']['width']) && $cfg['option']['width'] ? $cfg['option']['width'] : '80%';
        unset($cfg['option']['width']);
        // 字段提示信息
        $tips = isset($cfg['validate']['tips']) && $cfg['validate']['tips'] ? '<div class="onShow" id="dr_'.$name.'_tips">'.$cfg['validate']['tips'].'</div>' : '';
        // 字段默认值
        $value = $value ? dr_string2array($value) : array();
        // 禁止修改
        //$disabled = !IS_ADMIN && $id && $value && isset($cfg['validate']['isedit']) && $cfg['validate']['isedit'] ? 'disabled' : '';
        $str = '';
        // 加载js
        if (!defined('FINECMS_FILES_LD2')) {
            $str.= '<script type="text/javascript" src="/statics/js/jquery-ui.min.js"></script>';
            $str.= '<style>.form-horizontal .radio { padding-top: 3px;}</style>';
            define('FINECMS_FILES_LD2', 1);//防止重复加载JS
        }
        $str.= '<fieldset class="blue pad-10" style="width:'.$width.(is_numeric($width) ? 'px' : '').';">';
        $str.= '	<legend>'.$cname.'</legend>';
        $str.= '	<div class="picList" id="list_'.$name.'_property">';
        $str.= '		<div id="'.$name.'-sort-items">';

        $i = 0;

        $usucms = '<div id="dr_items_'.$name.'_{i}" style="border-bottom: 1px solid #dce3ed;padding-bottom: 10px;margin-bottom: 20px;">
        <div class="usu_item">
        <label>标题：</label>
        <label><input type="text" class="form-control input-xlarge" value="{name}" name="data['.$name.'][{i}][name]" /></label>
        <label><a href="javascript:$(\'#dr_items_'.$name.'_{i}\').remove();">删除</a></label>
        </div>
        <div class="usu_item">
        <label>类型：</label>
        <label>
        <div class="mt-radio-inline">
            <label class="mt-radio">
                <input type="radio" name="data['.$name.'][{i}][lx]" value="1" {lx1}> 单选
                <span></span>
            </label>
            <label class="mt-radio">
                <input type="radio" name="data['.$name.'][{i}][lx]" value="2" {lx2}> 多选
                <span></span>
            </label>
            <label class="mt-radio">
                <input type="radio" name="data['.$name.'][{i}][lx]" value="3" {lx3}> 内容
                <span></span>
            </label>
        </div>
        </label>
        </div>
        <div class="usu_item">
        <label>选项：</label>
        <label>
        <textarea class="form-control input-xlarge" rows="4" name="data['.$name.'][{i}][xx]">{xx}</textarea>
        
</label>
        </div>
        
</div>';

        // 已经保存的属性
        if ($value) {
            foreach ($value as $t) {
                $lx1 = $lx2 = $lx3 = '';
                if ($t['lx'] == 1) {
                    $lx1 = ' checked';
                } elseif ($t['lx'] == 2) {
                    $lx2 = ' checked';
                } elseif ($t['lx'] == 3) {
                    $lx3 = ' checked';
                }
                $str.= str_replace(array('{i}', '{name}', '{xx}', '{lx1}', '{lx2}', '{lx3}'), array($i, $t['name'], $t['xx'], $lx1, $lx2, $lx3), $usucms);
                $i++;
            }
        }

        $str.= '		</div>';
        $str.= '	</div>';
        $str.= '</fieldset>';
        $str.= '<div class="bk10"></div>';
        $str.= '<div class="">';
        $str.= '	<a href="javascript:;" class="btn blue btn-sm" onClick="dr_add_usucms_group_'.$name.'()"> <i class="fa fa-plus"></i> 添加组 </a>';
        $str.= '</div>';
        $str.= '<script type="text/javascript">
		$("#'.$name.'-sort-items").sortable();
		function dr_add_usucms_group_'.$name.'() {
			var i = ($("#'.$name.'-sort-items div").size() + 1) * 10;
			var json = '.json_encode(array('html' => $usucms)).';
			var html = json.html;
			html = html.replace(/{i}/g, i);
			html = html.replace("{name}","");
			html = html.replace("{xx}","");
			html = html.replace("{lx2}","");
			html = html.replace("{lx1}","");
			html = html.replace("{lx3}","");
			
			$("#'.$name.'-sort-items").append(html);
		}
		</script><span class="help-block">'.$tips.'</span>';
        return $this->input_format($name, $text, $str);
    }

}